package com.example.beautymakeup.controller;

import com.example.beautymakeup.domain.Code;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.domain.Result;
import com.example.beautymakeup.domain.entry.LoginUser;
import com.example.beautymakeup.pojo.User;
import com.example.beautymakeup.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public Result getById(@RequestHeader("Shop_token") String token) throws Exception {
        LoginUser byOpenId = userService.getById(token);
        if (byOpenId == null){
            return new Result(Code.GET_ERR,"查询失败");
        }
        return new Result(Code.GET_OK,"查询成功",byOpenId);
    }

    @PostMapping
    public Result save(@RequestBody User user){
        boolean save = userService.save(user);
        if (!save) {
            return new Result(Code.SAVE_ERR,"保存失败");
        }
        return new Result(Code.SAVE_OK,"保存成功");
    }

    @PutMapping
    public Result updateById(@RequestHeader("Shop_token") String token,@RequestBody User user) throws Exception {
        int i = userService.updateUser(token,user);
        if (i == 1){
            return new Result(Code.UPDATE_OK,"修改成功");
        }else {
            return new Result(Code.UPDATE_ERR,"修改失败");
        }
    }

    @GetMapping("/all")
    public Result getUserAll(@RequestHeader("Shop_token") String token,@RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<User> userAll = userService.getUserAll(token,page,size);
        if (userAll != null){
            return new Result(Code.GET_OK,"查询成功",userAll);
        }
        else{
            return new Result(Code.GET_ERR,"查询失败");
        }
    }

    @PostMapping("/ban")
    public Result banByUId(@RequestHeader("Shop_token") String token,@RequestBody User user) throws Exception {
        boolean b = userService.banByUId(token,user);
        if (b){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_ERR,"修改失败");
    }
}
